package com.xcx.controller.user;


import com.alibaba.fastjson.JSONArray;
import com.xcx.dto.StoreInspectionDetailDto;
import com.xcx.dto.StoreInspectionRecordDto;
import com.xcx.dto.StoreInspectionRecordPages;
import com.xcx.entity.StoreInspectionDetail;
import com.xcx.result.PageResult;
import com.xcx.result.Result;
import com.xcx.service.user.StoreInspectionDetailServiceImpl;
import com.xcx.service.user.StoreInspectionRecordServiceImpl;
import com.xcx.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/xcx/StoreInspection")
@Slf4j
public class StoreInspectionController {

    @Autowired
    private StoreInspectionDetailServiceImpl storeInspectionDetailService;
    @Autowired
    private StoreInspectionRecordServiceImpl storeInspectionRecordService;

    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping("/additem")
    public Result additem(@RequestBody StoreInspectionDetailDto storeInspectionDetailDto) {
        storeInspectionDetailService.additem(storeInspectionDetailDto);
        return Result.success();
    }

    @PostMapping("/updateitem")
    public Result updateitem(@RequestBody StoreInspectionDetailDto storeInspectionDetailDto){
        storeInspectionDetailService.updateitem(storeInspectionDetailDto);
        return Result.success();
    }

    @PostMapping("/queryitemByIdentity")
    public Result queryitemByIdentity(@RequestBody Map<String,Object> map){
        List<StoreInspectionDetail> list = storeInspectionDetailService.queryitemByIdentity(map);
        return Result.success(list);
    }

    @GetMapping("/getSubDetail")
    public Result getSubDetail(@RequestParam("subbh") String subbh){
        JSONArray list = storeInspectionDetailService.getSubDetail(subbh);
        return Result.success(list);
    }

    @PostMapping("/addRecord")
    public Result addRecord(@RequestBody StoreInspectionRecordDto storeInspectionRecordDto){
        storeInspectionRecordService.addRecord(storeInspectionRecordDto);
        return Result.success();
    }

    @PostMapping("/queryRecord")
    public Result<PageResult> queryRecord(@RequestBody StoreInspectionRecordPages storeInspectionRecordPages){
        PageResult pageResult = storeInspectionRecordService.queryRecord(storeInspectionRecordPages);
        return Result.success(pageResult);
    }

    @PostMapping("/exportRecord")
    public void exportRecord(HttpServletResponse response, @RequestBody StoreInspectionRecordPages storeInspectionRecordPages) throws Exception {
        storeInspectionRecordService.exportRecord(response,storeInspectionRecordPages);
    }


    @PostMapping("/upload")
    public Result upload(@RequestPart("file") MultipartFile file){
        try {
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));

            String fileNewName = UUID.randomUUID().toString() + extension;

            String filePath = aliOssUtil.upload(file.getBytes(), fileNewName);
            return Result.success(filePath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
